package com.flycms.module.comment.service;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.flycms.module.comment.dao.AnswersCountDao;
import com.flycms.module.comment.dao.AnswersVoteDao;
import com.flycms.module.comment.dao.QuestionsCountDao;
import com.flycms.module.comment.dao.QuestionsVoteDao;
import com.flycms.module.comment.model.AnswersOptionCount;
import com.flycms.module.comment.model.FlyContentType;
import com.flycms.module.comment.model.OptionCount;
import com.flycms.module.comment.model.Votes;
import com.flycms.module.qasystem.dao.AnswersDao;
import com.flycms.module.qasystem.dao.QuestionsDao;
import com.flycms.module.qasystem.model.Answers;

/**
 * 
 *
 * <br/>Copyright 2020
 * <br/>All rights reserved
 * @author 沁言訫語
 * @version 2020年3月8日
 * @Date 2020年3月8日
 */

@Service
public class VotesService {

    @Autowired
    protected QuestionsDao questionDao;
    
    @Autowired
    protected AnswersDao answerDao;
    
    @Autowired
    protected QuestionsVoteDao questionsVoteDao;
    
    @Autowired
    protected QuestionsCountDao questionsCountDao;
    
    @Autowired
    protected AnswersVoteDao answersVoteDao;
    
    @Autowired
    protected AnswersCountDao answersCountDao;
    
    @Transactional
    public OptionCount addVote(Votes vote, FlyContentType type) throws Exception {
        int i = 0;
        switch (type) {
          case QUESTION:
//              i = questionsVoteDao.addVote(vote);
              break;
          case ANSWER:
              i = answersVoteDao.addVote(vote);
              break;
          default: break;
        }
        OptionCount o = null;
        if (i != 0) {
            o = recordVoteCount(vote, type);
        }
        return o;
    }
    
    public OptionCount recordVoteCount(Votes vote, FlyContentType type) {
        OptionCount o = null;
        switch (type) {
          case QUESTION:
//              o = questionsVoteDao.queryVoteCount(vote.getContentId());
//              c = questionsCountDao.queryCountById(vote.getContentId());
//              if (c == null) {
//                  o.setId(vote.getContentId());
//                  o.setCollectCount(0);
//                  o.setCommentCount(0);
//                  questionsCountDao.addCount(o);
//              } else {
//                  c.setUpCount(o.getUpCount());
//                  c.setDownCount(o.getDownCount());
//                  questionsCountDao.updateCount(c);
//              }
              break;
          case ANSWER:
              o = answersVoteDao.queryVoteCount(vote.getContentId());
              AnswersOptionCount c = answersCountDao.queryCountById(vote.getContentId());
              Answers a = answerDao.queryAnswerById(vote.getContentId(), null);
              if (c == null) {
                  AnswersOptionCount aoc = new AnswersOptionCount();
                  aoc.setUpCount(o.getUpCount());
                  aoc.setDownCount(o.getDownCount());
                  aoc.setId(a.getId());
                  aoc.setQuestionId(a.getQuestionId());
                  aoc.setCommentCount(0);
                  aoc.setCollectCount(0);
                  answersCountDao.addCount(aoc);
              } else {
                  c.setUpCount(o.getUpCount());
                  c.setDownCount(o.getDownCount());
                  answersCountDao.updateCount(c);
              }
              
              int i = answersCountDao.sumCountByQuestionsId(a.getQuestionId(), "up");
              OptionCount oc = questionsCountDao.queryCountById(a.getQuestionId());
              if (oc == null) {
                  oc = new OptionCount(a.getQuestionId(), i, 0, 0, 0, 0);
                  questionsCountDao.addCount(oc);
              } else {
                  oc.setUpCount(i);
                  oc.setDownCount(0);
                  questionsCountDao.updateCount(oc);
              }
              break;
          default: break;
        }
        return o;
    }
    
    @Transactional
    public OptionCount updateVote(Votes vote, FlyContentType type) throws Exception {
        int i = 0;
        switch (type) {
          case QUESTION:
              i = questionsVoteDao.updateVote(vote);
              break;
          case ANSWER:
              i = answersVoteDao.updateVote(vote);
          default: break;
        }
        OptionCount o = null;
        if (i != 0) {
            o = recordVoteCount(vote, type);
        }
        return o;
    }
    
    public Votes queryVotedVote(Long userId, Long contentId, FlyContentType type) throws Exception {
        Votes vote = null;
        switch (type) {
          case QUESTION:
              vote = questionsVoteDao.queryVotedVote(userId, contentId);
              break;
          case ANSWER:
              vote = answersVoteDao.queryVotedVote(userId, contentId);
              break;
          default: break;
        }
        return vote;
    }
    
}
